home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / program / 441 / dlibs12 / getopt.c < prev    next >
C/C++ Source or Header  |  1990-11-23  |  3KB  |  134 lines

  1. /*
  2. From: gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>)
  3. Newsgroups: net.sources
  4. Subject: getopt library routine
  5. Date: 30 Mar 85 04:45:33 GMT
  6. */
  7. /*
  8.     getopt -- public domain version of standard System V routine
  9.  
  10.     Strictly enforces the System V Command Syntax Standard;
  11.     provided by D A Gwyn of BRL for generic ANSI C implementations
  12.  
  13.     Modified for dLibs on the Atari ST by Dale Schumacher, 07Oct88.
  14.     #define STRICT to prevent acceptance of clustered options with
  15.     arguments and ommision of whitespace between option and arg.
  16. */
  17.  
  18. #include    <stdio.h>
  19. #include    <string.h>
  20.  
  21. int    opterr = 1;            /* error => print message */
  22. int    optind = 1;            /* next argv[] index */
  23. char    *optarg = NULL;            /* option parameter if any */
  24.  
  25. static int
  26. Err( name, mess, c )            /* returns '?' */
  27.     char    *name;            /* program name argv[0] */
  28.     char    *mess;            /* specific message */
  29.     int    c;            /* defective option letter */
  30.     {
  31. #ifdef dLibs
  32.     char nbuf[16];
  33. #endif
  34.  
  35.     if ( opterr )
  36.         {
  37. #ifdef dLibs
  38.         _splitpath(name, NULL, NULL, nbuf, NULL);
  39.         name = nbuf;
  40. #endif
  41.         (void) fprintf( stderr,
  42.                 "%s: %s -- %c\n",
  43.                 name, mess, c
  44.                   );
  45.         }
  46.  
  47.     return '?';            /* erroneous-option marker */
  48.     }
  49.  
  50. int
  51. getopt( argc, argv, optstring )        /* returns letter, '?', EOF */
  52.     int        argc;        /* argument count from main */
  53.     char        *argv[];    /* argument vector from main */
  54.     char        *optstring;    /* allowed args, e.g. "ab:c" */
  55.     {
  56.     static int    sp = 1;        /* position within argument */
  57.     register int    osp;        /* saved `sp' for param test */
  58. #ifndef STRICT
  59.     register int    oind;        /* saved `optind' for param test */
  60. #endif
  61.     register int    c;        /* option letter */
  62.     register char    *cp;        /* -> option in `optstring' */
  63.  
  64.     optarg = NULL;
  65.  
  66.     if ( sp == 1 )            /* fresh argument */
  67.         if ( optind >= argc        /* no more arguments */
  68.           || argv[optind][0] != '-'    /* no more options */
  69.           || argv[optind][1] == '\0'    /* not option; stdin */
  70.            )
  71.             return EOF;
  72.         else if ( strcmp( argv[optind], "--" ) == 0 )
  73.             {
  74.             ++optind;    /* skip over "--" */
  75.             return EOF;    /* "--" marks end of options */
  76.             }
  77.  
  78.     c = argv[optind][sp];        /* option letter */
  79.     osp = sp++;            /* get ready for next letter */
  80.  
  81. #ifndef STRICT
  82.     oind = optind;            /* save optind for param test */
  83. #endif
  84.     if ( argv[optind][sp] == '\0' )    /* end of argument */
  85.         {
  86.         ++optind;        /* get ready for next try */
  87.         sp = 1;            /* beginning of next argument */
  88.         }
  89.  
  90.     if ( c == ':' || c == '?'    /* optstring syntax conflict */
  91.       || (cp = strchr( optstring, c )) == NULL    /* not found */
  92.        )
  93.         return Err( argv[0], "illegal option", c );
  94.  
  95.     if ( cp[1] == ':' )        /* option takes parameter */
  96.         {
  97. #ifdef STRICT
  98.         if ( osp != 1 )
  99.             return Err( argv[0],
  100.                     "option must not be clustered",
  101.                     c
  102.                   );
  103.  
  104.         if ( sp != 1 )        /* reset by end of argument */
  105.             return Err( argv[0],
  106.                    "option must be followed by white space",
  107.                     c
  108.                   );
  109.  
  110. #else
  111.         if ( oind == optind )    /* argument w/o whitespace */
  112.             {
  113.             optarg = &argv[optind][sp];
  114.             sp = 1;        /* beginning of next argument */
  115.             }
  116.  
  117.         else
  118. #endif
  119.         if ( optind >= argc )
  120.             return Err( argv[0],
  121.                     "option requires an argument",
  122.                     c
  123.                   );
  124.  
  125.         else            /* argument w/ whitespace */
  126.             optarg = argv[optind];
  127.  
  128.         ++optind;        /* skip over parameter */
  129.         }
  130.  
  131.     return c;
  132.     }
  133.  
  134.